package com.jctrip.hotel.source.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.greentree.req.*;
import com.jctrip.hotel.source.model.greentree.resp.*;
import com.jctrip.hotel.source.service.greentree.GreenTreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author：Haruhi
 * @description:
 * @Date：2023/8/30 18:18
 * @Package：com.jctrip.hotel.source.controller
 * @Project：hotel-main
 */
@Slf4j
@Api(tags = "greentree")
@RestController
@RequestMapping("/greentree")
@ApiSupport(order = 5)
public class GreenTreeController {
    @Resource
    private GreenTreeService greenTreeService;

    @Resource
    private RedisUtil redisUtil;
    @GetMapping("/test")
    @ApiOperation(value = "getAllHotelInfo", notes = "获取所有酒店编号及名称")
    public void test(@RequestParam String hotelId) {
        GetActivityRoomListReq getActivityRoomListReq = GetActivityRoomListReq.builder()
                .reqHotels(GetActivityRoomListReq.ReqHotels.builder()
                        .reqHotel(GetActivityRoomListReq.ReqHotels.ReqHotel.builder()
                                .hotelCode(hotelId)
                                .build())
                        .build())
                .build();
        // 获取活动房列表 , 获取价格码
        GetActivityRoomListResp activityRoomList = greenTreeService.getActivityRoomList(getActivityRoomListReq);
        // 遍历房型
        Optional.ofNullable(activityRoomList.getReqHotels().getReqHotel().getReqTypes().getReqTypeList())
                .orElse(new ArrayList<>())
                .forEach(e -> {
                    // 查询房态
                    GetAllRoomCountAndPriceActivityReq priceActivityReq =
                            GetAllRoomCountAndPriceActivityReq.builder()
                                    .hotelCode(hotelId)
                                    .startDate("2023-11-30")
                                    .endDate("2023-12-01")
                                    .reqRooms(GetAllRoomCountAndPriceActivityReq.ReqRooms.builder()
                                            .reqRoom(GetAllRoomCountAndPriceActivityReq.ReqRooms.ReqRoom.builder()
                                                    .activityCode(e.getActivityCode())
                                                    .roomTypeCode(e.getRoomTypeCode())
                                                    .build())
                                            .build())
                                    .build();
                    try {
                        GetAllRoomCountAndPriceActivityResp res = greenTreeService.getAllRoomCountAndPriceActivity(priceActivityReq);
                        log.info(JsonUtil.toString(res));
                    } catch (Exception f) {
                        log.error("活动价格码:"+e.getActivityCode() + " 房型编号-名称:" + e.getRoomTypeCode()+":"+e.getRoomTypeName() +" \n"+f.getMessage());
                    }
                });
    }

    @PostMapping("/getAllHotelInfo")
    @ApiOperation(value = "getAllHotelInfo", notes = "获取所有酒店编号及名称")
    @ApiOperationSupport(order = 1)
    public Result getAllHotelInfo(@RequestBody GetAllHotelInfoReq getAllHotelInfoReq) {
        GetAllHotelInfoResp result;
        try {
            result = greenTreeService.getAllHotelInfo(getAllHotelInfoReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getHotelInfo")
    @ApiOperation(value = "getHotelInfo", notes = "获取酒店信息")
    @ApiOperationSupport(order = 2)
    public Result getHotelInfo(@RequestBody GetHotelInfoReq getHotelInfoReq) {
        GetHotelInfoResp result;
        try {
            result = greenTreeService.getHotelInfo(getHotelInfoReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getRoomTypeList")
    @ApiOperation(value = "getRoomTypeList", notes = "获取酒店所有房型")
    @ApiOperationSupport(order = 3)
    public Result getRoomTypeList(@RequestBody GetRoomTypeListReq getRoomTypeListReq) {
        GetRoomTypeListResp result;
        try {
            result = greenTreeService.getRoomTypeList(getRoomTypeListReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getAllRoomCountAndPrice")
    @ApiOperation(value = "getAllRoomCountAndPrice", notes = "获取酒店所有房型的房价和房量（正价全量数据）")
    @ApiOperationSupport(order = 4)
    public Result getAllRoomCountAndPrice(@RequestBody GetAllRoomCountAndPriceReq getAllRoomCountAndPriceReq) {
        GetAllRoomCountAndPriceResp result;
        try {
            result = greenTreeService.getAllRoomCountAndPrice(getAllRoomCountAndPriceReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/bookingCheck")
    @ApiOperation(value = "bookingCheck", notes = "查询酒店房型是否可用（正价下单前校验）")
    @ApiOperationSupport(order = 5)
    public Result bookingCheck(@RequestBody BookingCheckReq bookingCheckReq) {
        BookingCheckResp result;
        try {
            result = greenTreeService.bookingCheck(bookingCheckReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/searchBooking")
    @ApiOperation(value = "searchBooking", notes = "查询预定状态（通用）")
    @ApiOperationSupport(order = 6)
    public Result searchBooking(@RequestBody SearchBookingReq searchBookingReq) {
        SearchBookingResp result;
        try {
            result = greenTreeService.searchBooking(searchBookingReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/createBooking")
    @ApiOperation(value = "createBooking", notes = "酒店预定(正价)")
    @ApiOperationSupport(order = 7)
    public Result createBooking(@RequestBody CreateBookingReq createBookingReq) {
        CreateBookingResp result;
        try {
            result = greenTreeService.createBooking(createBookingReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/cancelBooking")
    @ApiOperation(value = "cancelBooking", notes = "取消预定（通用）")
    @ApiOperationSupport(order = 8)
    public Result cancelBooking(@RequestBody CancelBookingReq cancelBookingReq) {
        CancelBookingResp result;
        try {
            result = greenTreeService.cancelBooking(cancelBookingReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getBreakfastInfo")
    @ApiOperation(value = "getBreakfastInfo", notes = "获取酒店早餐信息")
    @ApiOperationSupport(order = 9)
    public Result getBreakfastInfo(@RequestBody GetBreakfastInfoReq getBreakfastInfoReq) {
        GetBreakfastInfoResp result;
        try {
            result = greenTreeService.getBreakfastInfo(getBreakfastInfoReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getActivityRoomList")
    @ApiOperation(value = "getActivityRoomList", notes = "获取酒店所有活动房型（获取活动房列表）")
    @ApiOperationSupport(order = 10)
    public Result getActivityRoomList(@RequestBody GetActivityRoomListReq getActivityRoomListReq) {
        GetActivityRoomListResp result;
        try {
            result = greenTreeService.getActivityRoomList(getActivityRoomListReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/getAllRoomCountAndPriceActivity")
    @ApiOperation(value = "getAllRoomCountAndPriceActivity", notes = "获取酒店所有活动房价房量（活动全量）")
    @ApiOperationSupport(order = 11)
    public Result getAllRoomCountAndPriceActivity(@RequestBody GetAllRoomCountAndPriceActivityReq getAllRoomCountAndPriceActivityReq) {
        GetAllRoomCountAndPriceActivityResp result;
        try {
            result = greenTreeService.getAllRoomCountAndPriceActivity(getAllRoomCountAndPriceActivityReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/bookingCheckActivity")
    @ApiOperation(value = "bookingCheckActivity", notes = "查询酒店活动房型房量房价是否可用（活动下单前校验")
    @ApiOperationSupport(order = 12)
    public Result bookingCheckActivity(@RequestBody BookingCheckActivityReq bookingCheckActivityReq) {
        BookingCheckActivityResp result;
        try {
            result = greenTreeService.bookingCheckActivity(bookingCheckActivityReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/createBookingActivity")
    @ApiOperation(value = "createBookingActivity", notes = "酒店预定(活动房型)")
    @ApiOperationSupport(order = 13)
    public Result createBookingActivity(@RequestBody CreateBookingActivityReq createBookingActivityReq) {
        CreateBookingActivityResp result;
        try {
            result = greenTreeService.createBookingActivity(createBookingActivityReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }


    @PostMapping("/getAgreementPrice")
    @ApiOperation(value = "getAgreementPrice", notes = "获取酒店所有房型的房价和房量（协议全量数据）")
    @ApiOperationSupport(order = 14)
    public Result getAgreementPrice(@RequestBody GetAgreementPriceReq getAgreementPriceReq) {
        GetAgreementPriceResp result;
        try {
            result = greenTreeService.getAgreementPrice(getAgreementPriceReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/bookingCheckAgreement")
    @ApiOperation(value = "bookingCheckAgreement", notes = "查询酒店房型是否可用（协议下单前校验）")
    @ApiOperationSupport(order = 15)
    public Result bookingCheckAgreement(@RequestBody BookingCheckAgreementReq bookingCheckAgreementReq) {
        BookingCheckAgreementResp result;
        try {
            result = greenTreeService.bookingCheckAgreement(bookingCheckAgreementReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/createBookingAgreement")
    @ApiOperation(value = "createBookingAgreement", notes = "酒店预定(协议)")
    @ApiOperationSupport(order = 16)
    public Result createBookingAgreement(@RequestBody CreateBookingAgreementReq createBookingAgreementReq) {
        CreateBookingAgreementResp result;
        try {
            result = greenTreeService.createBookingAgreement(createBookingAgreementReq);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }
}
